package com.xsh.gls.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.gls.pojo.*;
import com.xsh.gls.service.FunctionalService;
import com.xsh.gls.service.Type_FunctionalService;
import com.xsh.gls.service.UserTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private UserTypeService userTypeService;
    @Autowired
    private FunctionalService functionalService;
    @Autowired
    private Type_FunctionalService type_functionalService;
    @RequestMapping("/userType")
    public PageInfo<User_Type> userType(@RequestParam(required = false)String pageNum){
        List<User_Type> listType=null;
        Integer _pageNum=1;
        if(pageNum!=null&&!pageNum.equals("")){
            _pageNum=Integer.parseInt(pageNum);
        }
        PageHelper.startPage(_pageNum,5 );
        try {
            listType=userTypeService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo<User_Type> page=new PageInfo<User_Type>(listType);
        return page;
    }

    @RequestMapping("/toRole")
    public List<Functional> toRole(){
        List<Functional> list=null;
        try {
            list=  functionalService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    @RequestMapping("/typefun")
    public List<Type_Functional> typefun(Integer typeId){
        List<Type_Functional> list=null;
        try {
            list=  type_functionalService.queryFunctionByTypeId(typeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    @RequestMapping("/updateRole")
    public Transfer updateRole(Integer typeId,int[] checkIDs){
        Transfer transfer=new Transfer();
        try {
            if(type_functionalService.updateRole(typeId,checkIDs)){
                transfer.setMessage("yes");
            }else{
                transfer.setMessage("no");
            }
        } catch (Exception e) {
            e.printStackTrace();
            transfer.setMessage("no");
        }
        return transfer;
    }

    @RequestMapping("/delType")
    public Transfer delType(Integer typeId){
        Transfer transfer=new Transfer();
        try {
            if(userTypeService.delById(typeId)){
                transfer.setMessage("yes");
            }else{
                transfer.setMessage("no");
            }
        } catch (Exception e) {
            e.printStackTrace();
            transfer.setMessage("no");
        }
        return transfer;
    }
}
